package com.zhss.demo.register.server;

import java.util.LinkedList;
import java.util.Map;

/**
 * 这个controller是负责接收register-client发送过来的请求的
 * 在Spring Cloud Eureka中用的组件是jersey，百度一下jersey是什么东西
 * 在国外很常用jersey，restful框架，可以接受http请求
 *
 * @author zhonghuashishan
 */
public class RegisterServerController {

    private ServiceRegistry registry = ServiceRegistry.getInstance();
    private ServiceRegistryCache serviceRegistryCache=ServiceRegistryCache.getInstance();
    /**
     * 服务注册
     *
     * @param registerRequest 注册请求
     * @return 注册响应
     */
    public RegisterResponse register(RegisterRequest registerRequest) {
        RegisterResponse registerResponse = new RegisterResponse();

        try {
            ServiceInstance serviceInstance = new ServiceInstance();
            serviceInstance.setHostname(registerRequest.getHostname());
            serviceInstance.setIp(registerRequest.getIp());
            serviceInstance.setPort(registerRequest.getPort());
            serviceInstance.setServiceInstanceId(registerRequest.getServiceInstanceId());
            serviceInstance.setServiceName(registerRequest.getServiceName());

            registry.register(serviceInstance);
            //注册时将缓存中的ReadWriteCache全部失效，用于缓存的重新加载
            serviceRegistryCache.invalidate();
            registerResponse.setStatus(RegisterResponse.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            registerResponse.setStatus(RegisterResponse.FAILURE);
        }

        return registerResponse;
    }

    /**
     * 发送心跳
     *
     * @param heartbeatRequest 心跳请求
     * @return 心跳响应
     */
    public HeartbeatResponse heartbeat(HeartbeatRequest heartbeatRequest) {
        HeartbeatResponse heartbeatResponse = new HeartbeatResponse();

        try {
            // 对服务实例进行续约
            ServiceInstance serviceInstance = registry.getServiceInstance(
                    heartbeatRequest.getServiceName(),
                    heartbeatRequest.getServiceInstanceId());
            if (serviceInstance != null) {
                serviceInstance.renew();
            }

            // 记录一下每分钟的心跳的次数
            HeartbeatCounter heartbeatMessuredRate = new HeartbeatCounter();
            heartbeatMessuredRate.increment();

            heartbeatResponse.setStatus(HeartbeatResponse.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            heartbeatResponse.setStatus(HeartbeatResponse.FAILURE);
        }

        return heartbeatResponse;
    }

    /**
     * 拉取服务注册表
     *
     * @return
     */
    public Applications fetchFullServiceRegistry() {
        return (Applications) serviceRegistryCache.get(ServiceRegistryCache.RegistryType.FULL_REGISTER_SERVICE.toString());
    }

    /**
     * 获取增量服务实例注册表
     *
     * @return
     */
    public DeltaRegister fetchDeltaRegistry() {
        return (DeltaRegister) serviceRegistryCache.get(ServiceRegistryCache.RegistryType.DELTA_REGISTER_SERVICE.toString());
    }

    /**
     * 服务下线
     */
    public void cancel(String serviceName, String serviceInstanceId) {
        // 从服务注册中摘除实例
        registry.remove(serviceName, serviceInstanceId);
        // 过期掉注册表缓存
        serviceRegistryCache.invalidate();
    }
}
